package com.onemore.task.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;
import com.onemore.task.JdbcGlobal;

public class ModelVendor extends JdbcGlobal{

    final static private String vendor_field[] = new String[] {"GHSDM","GHSMC","XZDM","QDDM","LBDM",
    		"QYDM","YGDM","DZ","LXR","FZR","YB","DH1","DH2","SJ","CZ","EMAIL","WEB",
    		"KHH","ZH","SH","FR","HJKM","JGSD","ZK","BZ","TZSY","BYZD1","BYZD2","BYZD11","BYZD12",
    		"Createer","BYZD8","BYZD6","BYZD4",
    		"CreateDate","ModifyDate","FPLX"};
    static public boolean checkVendorValid(String GHSDM) throws SQLException
    {
    	if(search("GONGHUOSHANG", "GHSDM='"+GHSDM+"' AND TZSY!=1")) {
    		return true;
    	}
    	SQLException e = new SQLException("供货商代码在数据库找不到对应的供货商信息或该供货商未启用");
    	throw e;
    	
    }
    /**
     * 插入商品
     * @param json
     * @return
     * @throws SQLException 
     */
    static public boolean insert(JSONObject json) throws SQLException
    {
    	String fieldStr = "";
    	String valuesStr = "";
    	String updateStr = "";
    	List<String> listFields = new ArrayList<String>();
    	boolean isHas = false;
    	if(search("GONGHUOSHANG", vendor_field[0]+"='"+json.getString(vendor_field[0])+"'")) {
    		setDefault(json,"ModifyDate",df.format(new Date()));
	    	setDefault(json,"Modifyer","SCM");
    		isHas = true;
	    	if(!search("GONGHUOSHANG", vendor_field[0]+"='"+json.getString(vendor_field[0])+"' AND "
	    			+vendor_field[1]+"='"+json.getString(vendor_field[1])+"'")) {
	    		SQLException e = new SQLException("已经有相同的供货商代码，并且供货商名称不相同");
	    		throw e;
	    	}
    	}else {
    		setDefault(json,"CreateDate",df.format(new Date()));
	    	setDefault(json,"Createer","SCM");
    		isHas = false;
	    	if(search("GONGHUOSHANG", vendor_field[1]+"='"+json.getString(vendor_field[1])+"'")) {
	    		SQLException e = new SQLException("已经有相同的供货商名称了，此数据拒绝被插入数据库。");
	    		throw e;
	    	}
    	}
    	int i = 0;
    	for(i=0;i<vendor_field.length;i++) {
    		if(json.containsKey(vendor_field[i]))
    			listFields.add(vendor_field[i]);
    	}
    	for(i=0;i<listFields.size()-1;i++) {
    		fieldStr += listFields.get(i)+",";
    		valuesStr += "'"+json.getString(listFields.get(i))+"',";
    		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"',";
    	}
		fieldStr += listFields.get(i);
		valuesStr += "'"+json.getString(listFields.get(i))+"'";
		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"'";

		//delete("GONGHUOSHANG",vendor_field[0]+"='"+json.getString(vendor_field[0])+"'");
		String sql = "";
		if(isHas) {
			return false;
			//SQLException e = new SQLException("供货商数据已存在，拒绝插入数据");
			//throw e;
			//updateDate("GONGHUOSHANG",vendor_field[0]+"='"+json.getString(vendor_field[0])+"'",updateStr);
		}else {
			//查询测试
	        sql = "insert into GONGHUOSHANG ("+fieldStr+") values("+valuesStr+")";
			Logger.debug(sql);
			getJdbcOperate().update(sql);
		}
        //查询测试
        /*sql = "insert into GONGHUOSHANG ("+fieldStr+") values("+valuesStr+")";
        Map<String, Object> map = null;
		Logger.debug(sql);
		jOperate.update(sql);*/
		return true;
    }
    
}
